const { ListToNode, NodeToList, ListNode, timeL } = require("../utils.js");
/**
 * @param {number} n
 * @param {number} k
 * @return {string}
 */
var getPermutation = function (n, k) {
  let res = [];
  const nums = [];
  for (let i = 0; i < n; i++) {
    nums.push(i + 1);
  }
  const fun = (nums, arr = "") => {
    if (!nums.length) {
      res.push(arr);
      return;
    }
    if (res.length > k) return;
    for (let i = 0; i < nums.length; i++) {
      const list = [...nums.slice(0, i), ...nums.slice(i + 1)];
      // const list = [...nums]; //[...nums.slice(0, i), ...nums.slice(i + 1)];
      // list.splice(i, 1);
      fun(list, arr + nums[i]);
    }
  };
  fun(nums);
  return res[k - 1];
};
let n = 8,
  k = 1907;

timeL(getPermutation, [n, k]);
